package linkedList;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByNo(heroNode1);
        singleLinkedList.addByNo(heroNode4);
        singleLinkedList.addByNo(heroNode2);
        singleLinkedList.addByNo(heroNode3);
        singleLinkedList.list();
        HeroNode newHeroNode = new HeroNode(3, "吴用~~", "智多星~~");
        singleLinkedList.update(newHeroNode);
        singleLinkedList.list();
        /*singleLinkedList.delete(3);
        singleLinkedList.list();
        System.out.println("链表长度为：" + getLength(singleLinkedList.getHead()));
        singleLinkedList.delete(2);
        singleLinkedList.list();
        System.out.println("链表长度为：" + getLength(singleLinkedList.getHead()));*/
       /* System.out.println("~~~~~~~~");
        HeroNode lastIndexNode = getLastIndexNode(singleLinkedList.getHead(), 2);
        System.out.println(lastIndexNode);
        System.out.println("~~~~~~~~~~");
        System.out.println(getLastIndexNode(singleLinkedList.getHead(), 1));
*/
        System.out.println("反转前：");
        singleLinkedList.list();
        System.out.println("反转后：");
        singleLinkedList.setHead(reverse(singleLinkedList.getHead()));
        singleLinkedList.list();

    }

    /**
     * 获得链表长度
     *
     * @param head 传递的是头节点
     * @return
     */
    public static int getLength(HeroNode head) {
        HeroNode temp = head;
        //头节点不算长度
        int length = 0;
        if (temp.next == null) {
            //链表为空
            return 0;
        }
        while (temp.next != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

    /**
     * 获得链表中倒数第K个节点
     *
     * @param head
     * @param index
     */
    public static HeroNode getLastIndexNode(HeroNode head, int index) {
        int length = getLength(head);
        if (length == 0) {
            System.out.println("链表为空");
            return null;
        }
        if (index < 0 || index > length) {
            System.out.println("链表长度为：" + length + "，倒数第" + index + "个节点不存在");
            return null;
        }
        //创建临时节点
        HeroNode temp = head;
        for (int i = 0; i < length - index; i++) {
            temp = temp.next;
        }
        return temp.next;

    }

    /**
     * 链表反转 参数为头节点
     *
     * @param head
     * @return
     */
    public static HeroNode reverse(HeroNode head) {
        if (head.next == null || head.next.next == null) {
            //链表为空或只有一个节点
            return head;
        }
        //创建一个新链表结点的头
        SingleLinkedList revserseHead = new SingleLinkedList();
        //创建一个临时节点
        HeroNode temp = head.next;
        //创捷一个节点保存循环到的节点 防止链表丢失
        HeroNode tempNode = null;
        while (temp != null) {
            //保存遍历到的节点
            if (temp.next != null) {
                tempNode = temp.next;
                revserseHead.addHead(temp);
                temp = tempNode;
            }else {
                revserseHead.addHead(temp);
                break;
            }

        }
        return revserseHead.getHead();

    }
}

//这个类相当于先创建头节点 需要插入节点就直接调用add
class SingleLinkedList {
    //先创建头节点 头节点不存数据 只是为了指明下一个节点地址 然后该节点位置必须一直保存 不然就找不到这个链表
    HeroNode head = new HeroNode(0, "", "");

    //获得头节点
    public HeroNode getHead() {
        return head;
    }

    public void setHead(HeroNode head) {
        this.head = head;
    }

    //添加节点到单向链表
    public void add(HeroNode heroNode) {
        //因为头节点需要一直保存 所以先找一个临时节点代替头节点向下找末尾
        HeroNode temp = head;
        while (true) {
            //该节点是末尾节点
            if (temp.next == null) {
                break;
            }
            //该节点不是末尾节点 继续往下找
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //遍历链表
    public void list() {
        //先判断这个链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //创建一个临时节点代替头节点
        HeroNode temp = head.next;
        while (true) {
            System.out.println(temp);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
    }

    //根据no大小顺序来添加节点
    public void addByNo(HeroNode heroNode) {
        //还是先创建头节点的临时代替
        HeroNode temp = head;
        boolean flag = false;//标识添加的节点是否已经存在
        while (true) {
            if (temp.next == null) {
                //该节点是末尾节点
                break;
            }
            if (temp.next.no == heroNode.no) {
                //该节点已经存在
                flag = true;
                break;
            }
            if (temp.next.no > heroNode.no) {
                //找到插入位置
                break;
            }
            //继续往下找
            temp = temp.next;
        }
        if (flag) {
            System.out.println("该节点已经存在");
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    public void update(HeroNode newHeroNode) {
        //还是先找头节点的临时节点代替
        HeroNode temp = head;
        //判断是否为空链表
        if (temp.next == null) {
            System.out.println("链表为空");
            return;
        }
        while (true) {

            if (temp.next.no == newHeroNode.no) {
                //找到了
                break;
            }
            if (temp.next == null) {
                //该节点是末尾节点 说明没找到
                break;
            }
            temp = temp.next;
        }
        if (temp.next != null) {
            //找到了要修改的那个
            temp.next.name = newHeroNode.name;
            temp.next.nickname = newHeroNode.nickname;
            System.out.println("修改成功");
        } else {
            System.out.println("没有找到该节点");
        }
    }

    public void delete(int no) {
        //先找头节点的临时节点代替
        HeroNode temp = head;
        boolean flag = false;
        if (temp.next == null) {
            System.out.println("链表为空");
            return;
        }
        while (true) {
            if (temp.next == null) {
                //该节点是末尾节点 说明没找到
                break;
            }
            if (temp.next.no == no) {
                //找到了
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            //说明找到了
            temp.next = temp.next.next;
            System.out.println("删除成功");
        } else {
            System.out.println("没有找到该节点");
        }
    }

    //头节点插入法
    public void addHead(HeroNode heroNode) {
        //创建临时节点
        HeroNode temp = head;
        heroNode.next = temp.next;
        temp.next = heroNode;
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点 默认为空 说明是最后一个节点

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
